package org.brisling.workflow.service;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.brisling.base.domain.ListDomain;
import org.brisling.workflow.domain.VOProcessDeploy;
import org.brisling.workflow.service.base.BaseWorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/deploy")
public class WorkFlowDeployService extends BaseWorkflowService{
	
	@Autowired
	private RepositoryService repositoryService;
	public WorkFlowDeployService(){
		this.moduleName = "deploy";
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	
	 
	@RequestMapping(value = "/getList")
    public List<Deployment> processDeployList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows) {
        
		
		if((page!=null)&&(page>0)){
			page -= 1;
		}else{
			page = 0;
		}
		
		if((rows==null)||(rows<=0)){
			rows = 10;
		}
	 	List<Deployment> deployList = new ArrayList<Deployment>();        
        deployList = repositoryService.createDeploymentQuery().listPage(page, rows);      
        return unCycleSelfReference(deployList);
    }
	
	private List<Deployment> unCycleSelfReference(List<Deployment> processDefinitionList){
		
		List<Deployment> retList = new ArrayList<Deployment>();
		VOProcessDeploy vop = null;		
		
		if((processDefinitionList!=null)&&(processDefinitionList.size()>0)){
			for(Deployment entity:processDefinitionList){
				vop = new VOProcessDeploy();
				vop.setDeploymentTime(entity.getDeploymentTime());
				
				vop.setId(entity.getId());
				
				vop.setCategory(entity.getCategory());				
				
				
				vop.setName(entity.getName());
				
				vop.setTenantId(entity.getTenantId());			
				
				retList.add(vop);
			}
		}
		
		return retList;
	}
	 
	@RequestMapping(value = "/getListDomain")
    public ListDomain<Deployment> processDeployListDomain(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows) {
       

        List<Deployment> processDeploymentList = this.processDeployList(page,rows);
       
        ListDomain<Deployment> listDomain = new ListDomain<Deployment>();
        if((processDeploymentList!=null)&&(processDeploymentList.size()>0)){
        	listDomain.setTotal((long) processDeploymentList.size());
        	listDomain.setRows(processDeploymentList);
        }else{
        	listDomain.setTotal(0L);
        	listDomain.setRows(new ArrayList<Deployment>());
        }
        return listDomain;
    }

}
